import json
import requests


class DingDingGroupRobot(object):
    """
    叮叮机器人提醒
    """

    @staticmethod
    def send_data(warning_msg, types=None):
        headers = {
            "Content-Type": "application/json"
        }
        url = "https://oapi.dingtalk.com/robot/send"
        params = {
            "access_token": "7195b2aa7f6b0fbf6ad360bf6d3344969fb2f8dbe40259020b462e163795a445"
        }
        data = {
            "msgtype": "markdown",
            "markdown": {
                'title': "容器监控预警",
                "text": warning_msg
            }
        }
        if types:
            data = {
                "msgtype": "markdown",
                "markdown": {
                    "title": "容器监控预警",
                    "text": warning_msg
                },
                "at": {
                    "atMobiles": [
                        "18217772447"
                    ],
                        # "atUserIds": [
                        #     "vw8q44j"
                        # ],
                    "isAtAll": False
                }
            }

        data = json.dumps(data)
        requests.post(url, headers=headers, params=params, data=data)


if __name__ == '__main__':
    dd = DingDingGroupRobot()
    msg = '<font color=\"warning\">容器监控预警</font>\n\n' \
          '> 内核名称：<font color=\"comment\">crawler_trace_air_CI</font>\n\n' \
          '> 问题描述：<font color=\"comment\">容器进程疑似Block，无法接受调度任务</font>\n\n' \
          '> 发生时间：<font color=\"comment\">2022-05-20 15:27:59</font>'
    print(dd.send_data(warning_msg=msg))
